{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "# 定义生命体类\n",
    "class LifeForm:\n",
    "    def __init__(self, speed, stamina):\n",
    "        self.speed = speed\n",
    "        self.stamina = stamina\n",
    "\n",
    "    # 模拟生命体的行为\n",
    "    def survive(self):\n",
    "        # 假设生存能力取决于速度和耐力的平衡\n",
    "        return self.speed * self.stamina\n",
    "\n",
    "# 选择函数\n",
    "def selection(population, fitness_scores):\n",
    "    # 轮盘赌选择\n",
    "    total_fitness = sum(fitness_scores)\n",
    "    probabilities = [score / total_fitness for score in fitness_scores]\n",
    "    selected_indices = []\n",
    "\n",
    "    for _ in range(len(population)):\n",
    "        r = random.random()\n",
    "        cumulative_probability = 0\n",
    "        for i, p in enumerate(probabilities):\n",
    "            cumulative_probability += p\n",
    "            if r < cumulative_probability:\n",
    "                selected_indices.append(i)\n",
    "                break\n",
    "\n",
    "    return selected_indices\n",
    "\n",
    "# 初始化种群\n",
    "population_size = 100\n",
    "population = [LifeForm(random.randint(1, 10), random.randint(1, 10)) for _ in range(population_size)]\n",
    "\n",
    "# 进化参数\n",
    "generations = 100  # 迭代代数\n",
    "mutation_rate = 0.1  # 变异率\n",
    "\n",
    "# 进化过程\n",
    "for generation in range(generations):\n",
    "    # 评估适应度\n",
    "    fitness_scores = [life_form.survive() for life_form in population]\n",
    "\n",
    "    # 选择\n",
    "    selected_indices = selection(population, fitness_scores)\n",
    "    selected_forms = [population[i] for i in selected_indices]\n",
    "\n",
    "    # 交叉和变异\n",
    "    offspring = []\n",
    "    for _ in range(len(population) // 2):  # 假设每次迭代产生一半的新生命体\n",
    "        parent1 = random.choice(selected_forms)\n",
    "        parent2 = random.choice(selected_forms)\n",
    "\n",
    "        # 交叉\n",
    "        child1 = LifeForm(\n",
    "            (parent1.speed + parent2.speed) // 2,\n",
    "            (parent1.stamina + parent2.stamina) // 2\n",
    "        )\n",
    "\n",
    "        # 变异\n",
    "        if random.random() < mutation_rate:\n",
    "            child1.speed += random.randint(-1, 1)\n",
    "            child1.stamina += random.randint(-1, 1)\n",
    "\n",
    "        offspring.append(child1)\n",
    "\n",
    "    # 新一代种群\n",
    "    population = offspring\n",
    "\n",
    "# 打印最终种群\n",
    "for life_form in population:\n",
    "    print(f\"Speed: {life_form.speed}, Stamina: {life_form.stamina}\")\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
